<?php
/**
 * Created by PhpStorm.
 * User: wg
 * Date: 16/4/18
 * Time: 下午4:18
 */
namespace C\mod{

    use C\lib\Db;
    use C\lib\Sql;
    class order extends mod {

        protected static $__table_s = 'order';
        protected static $__primary_s = 'id';
        /*
         * 获取符合条件的数据有多少行
         */
        public static function row($state,$worker_id){
            Db::connect();
            $count=Db::query("select count(id) as num from bd_order WHERE worker_id=".$worker_id." and state=".$state);
            $num = $count->fetch(\PDO::FETCH_ASSOC);
            //var_dump($num);exit;
            return $num;
        }

        /*订单号*/
        public static function build_order_no()
        {
            return 'BD' . date('Ymd') . substr(implode(NULL, array_map('ord', str_split(substr(uniqid(), 7, 13), 1))), 0, 8);
        }


        /*
         * 通过经纬度判断是否在范围内,返回当前位置为圆心5公里内的站点集
         * @$lat 经度
         * @lng 纬度
         * */
        static public function scope($lat, $lng,$state) {
            Db::connect();
            $sql = new Sql();
            $sql->select('*', static::$__table_s)->append(' WHERE sqrt((((' . ($lng + 0) . '-lng)*PI()*12656*cos(((' . ($lat + 0) . '+lat)/2)*PI()/180)/180)*((' . ($lng + 0) . '-lng)*PI()*12656*cos(((' . ($lat + 0) . '+lat)/2)*PI()/180)/180))+(((' . ($lat + 0) . '-lat)*PI()*12656/180)*((' . ($lat + 0) . '-lat)*PI()*12656/180)))<2 AND state='.$state);
            if ($ps = Db::query($sql->sql)) {
                $list = [];
                while ($o = $ps->fetchObject(static::class))
                    $list[] = $o;
                return $list;
            } else
                return false;
        }
    }

}

